import {Injectable} from '@angular/core';
import {Charge} from '../types/resp/Charge';
import {ChargeForm} from '../types/req/ChargeForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {ChargeQueryForm} from '../types/req/ChargeQueryForm';

@Injectable({
  providedIn: 'root'
})
export class ChargeService {
  charges: Charge[] = [
    {id: 1, buildingId: 1, buildingName: '北区1栋', roomId: 1, roomNumber: '301', chargeType: 40, billingCycle: '月度', billingStartDate: '2024-01-01', billingEndDate: '2024-01-31', amount: 32.54, lateFee: 0, dueDate: '2020-01-31', paymentDate: '', paymentStatus: 10, createTime: '2024-01-01 12:12:12', modifyTime: '2024-02-02 13:13:13'},
    {id: 2, buildingId: 2, buildingName: '北区1栋', roomId: 2, roomNumber: '402', chargeType: 20, billingCycle: '月度', billingStartDate: '2024-02-01', billingEndDate: '2024-02-29', amount: 43.54, lateFee: 2.36, dueDate: '2021-02-29', paymentDate: '2022-03-15', paymentStatus: 20, createTime: '2024-02-02 12:12:12', modifyTime: '2024-03-03 13:13:13'},
    {id: 3, buildingId: 3, buildingName: '南区1栋', roomId: 3, roomNumber: '503', chargeType: 30, billingCycle: '年度', billingStartDate: '2024-03-01', billingEndDate: '2024-03-31', amount: 54.54, lateFee: 0, dueDate: '2022-03-31', paymentDate: '', paymentStatus: 90, createTime: '2024-03-03 12:12:12', modifyTime: '2024-04-04 13:13:13'},
    {id: 4, buildingId: 4, buildingName: '南区2栋', roomId: 4, roomNumber: '604', chargeType: 10, billingCycle: '月度', billingStartDate: '2024-04-01', billingEndDate: '2024-04-30', amount: 65.54, lateFee: 4.58, dueDate: '2023-04-30', paymentDate: '2023-05-27', paymentStatus: 20, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 5, buildingId: 5, buildingName: '东区1栋', roomId: 5, roomNumber: '604', chargeType: 20, billingCycle: '年度', billingStartDate: '2024-04-01', billingEndDate: '2024-04-30', amount: 65.54, lateFee: 4.58, dueDate: '2022-04-30', paymentDate: '2021-05-27', paymentStatus: 10, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 6, buildingId: 6, buildingName: '东区2栋', roomId: 6, roomNumber: '604', chargeType: 30, billingCycle: '月度', billingStartDate: '2024-04-01', billingEndDate: '2024-04-30', amount: 65.54, lateFee: 4.58, dueDate: '2021-04-30', paymentDate: '2020-05-27', paymentStatus: 20, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'}
  ]
  constructor() {
  }

  save (chargeForm: ChargeForm): Observable<Result<void>> {
    console.log('chargeApi save params', chargeForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (chargeQueryForm: ChargeQueryForm): Observable<Result<Page<Charge>>> {
    console.log('chargeApi find params', chargeQueryForm)
    return MockApi.queryPageSuccessfully(this.charges).pipe(
      map((response: Response<Result<Page<Charge>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Charge>> {
    console.log('chargeApi findById id', id)
    let target = {}
    for (const a of this.charges) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Charge>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('chargeApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
